﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using ChinaSoft.BLL;
using ChinaSoft.Model;

namespace Admin.Sys.Resource
{
    public partial class Resource_Edit : System.Web.UI.Page
    {
        private BLL_T_SYS_RESOURCE bllResource = new BLL_T_SYS_RESOURCE();
        private Model_T_SYS_RESOURCE modelResource = new Model_T_SYS_RESOURCE();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTreeView();
                ControlBtn(false, false, false, false, false);
                ControlTextBox(true, true, true,false,false,true);
            }
        }
        /// <summary>
        /// 控制控件的状态（禁用、启用）
        /// </summary>
        /// <param name="btnAdd"></param>
        /// <param name="btnDel"></param>
        /// <param name="btnUpdate"></param>
        /// <param name="btnSave"></param>
        /// <param name="btnCancel"></param>
        private void ControlBtn(bool btnAdd, bool btnDel, bool btnUpdate, bool btnSave, bool btnCancel)
        {
            imgBtnAdd.Enabled = btnAdd;
            imgBtnDel.Enabled = btnDel;
            imgBtnUpdate.Enabled = btnUpdate;
            imgBtnSave.Enabled = btnSave;
            imgBtnCancel.Enabled = btnCancel;
        }
        /// <summary>
        /// 绑定树性菜单
        /// </summary>
        private void BindTreeView()
        {
            TreeView1.Nodes.Clear();
            ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
            DataSet ds = bllBind.GetDataSet("select t.parent_id,t.name,t.sys_resource_id,t.seq_no from t_sys_resource t group by t.level_no,t.parent_id,t.seq_no,t.name,t.sys_resource_id order by t.level_no,t.parent_id,t.seq_no");
            ChinaSoft.BLL.Common.TreeViewOperate bllTreeView = new ChinaSoft.BLL.Common.TreeViewOperate();
            bllTreeView.BindTreeView(ds, TreeView1, "NAME", "sys_resource_id", "parent_id", false);
        }
        /// <summary>
        /// 当树形菜单选择改变时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            ControlBtn(true, true, true, false, false);
            string resourceId = ChinaSoft.Common.Input.SetString(TreeView1.SelectedNode.Value);
            txtName.Value = TreeView1.SelectedNode.Text;
            modelResource = bllResource.GetModel(resourceId);
            if (modelResource != null)
            {
                txtCode.Value = modelResource.CODE;
                ddlResourceType.SelectedValue = ChinaSoft.Common.Input.SetString(modelResource.TYPE);
                txtSortNum.Value =ChinaSoft.Common.Input.SetString(modelResource.SEQ_NO);
                txtURL.Text = modelResource.URL;
            }
            
            ControlTextBox(true, true, true,false,false,false);
            HiddenResourceId.Value = resourceId;
        }
        
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
        {
            ControlBtn(true, true, false, true, true);
            ControlTextBox(false, false,false,true,true, true);
            HiddenResourceId.Value = "";
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnDel_Click(object sender, ImageClickEventArgs e)
        {
            string sourceId = ChinaSoft.Common.Input.SetString(TreeView1.SelectedNode.Value);
            bool result=bllResource.Delete(sourceId);
            if (result)
            {
                ChinaSoft.Common.MessageBox.Show(this, "删除成功！");
                ControlTextBox(false, false, false, true, true, true);
                BindTreeView();
                HiddenResourceId.Value = "";
            }
            else
            {
                ChinaSoft.Common.MessageBox.Show(this, "该数据已经不存在");
                BindTreeView();
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnUpdate_Click(object sender, ImageClickEventArgs e)
        {
            ControlBtn(true, true, false, true, true);
            ControlTextBox(false, false, false,true,true,false);
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
        {
            if (HiddenResourceId.Value == "")
            {
                #region 添加
                modelResource.SYS_RESOURCE_ID = Guid.NewGuid().ToString();
                modelResource.CODE = txtCode.Value.Trim();
                modelResource.CREATE_TIME = DateTime.Now;
                modelResource.CREATOR_ID = ChinaSoft.Common.Input.SetString(Session["currentUserID"]);
                modelResource.CREATOR_NAME = ChinaSoft.Common.Input.SetString(Session["UserName"]);
                modelResource.DECODE = "utf-8";
                modelResource.LEVEL_NO = TreeView1.SelectedNode.Depth+1;
                modelResource.NAME = txtName.Value.Trim();
                modelResource.PARENT_ID = ChinaSoft.Common.Input.SetString(HiddenFatherId.Value);
                modelResource.UPDATE_TIME = DateTime.Now;
                modelResource.UPDATOR_ID = ChinaSoft.Common.Input.SetString(Session["currentUserID"]);
                modelResource.UPDATOR_NAME = ChinaSoft.Common.Input.SetString(Session["UserName"]);
                modelResource.SEQ_NO = ChinaSoft.Common.Input.SetDecimal(txtSortNum.Value.Trim());
                modelResource.RESTYPE_ID = "";
                modelResource.TYPE = ChinaSoft.Common.Input.SetDecimal(ddlResourceType.SelectedItem.Value);
                modelResource.URL = txtURL.Text.Trim();
                bllResource.Add(modelResource);
                #endregion
                ChinaSoft.Common.MessageBox.Show(this, "添加成功！");
                BindTreeView();
                ControlTextBox(true, true, true,false,false,true);
            }
            else
            {
                    modelResource = bllResource.GetModel(HiddenResourceId.Value);
                    #region 更新
                    if (string.IsNullOrEmpty(modelResource.RESTYPE_ID))
                        modelResource.RESTYPE_ID = " ";
                    if (string.IsNullOrEmpty(modelResource.CREATOR_ID))
                        modelResource.CREATOR_ID = "";
                    if (string.IsNullOrEmpty(modelResource.CREATOR_NAME))
                        modelResource.CREATOR_NAME = "";
                    modelResource.LEVEL_NO = TreeView1.SelectedNode.Depth;
                    modelResource.NAME = txtName.Value.Trim();
                    modelResource.PARENT_ID = ChinaSoft.Common.Input.SetString(HiddenFatherId.Value);
                    modelResource.CODE = txtCode.Value.Trim();
                    modelResource.TYPE = ChinaSoft.Common.Input.SetDecimal(ddlResourceType.SelectedItem.Value);
                    modelResource.SEQ_NO = ChinaSoft.Common.Input.SetDecimal(txtSortNum.Value.Trim());
                    modelResource.URL = txtURL.Text.Trim();
                    modelResource.UPDATE_TIME = DateTime.Now;
                    modelResource.UPDATOR_ID = ChinaSoft.Common.Input.SetString(Session["currentUserID"]);
                    modelResource.UPDATOR_NAME = ChinaSoft.Common.Input.SetString(Session["UserName"]);
                    bllResource.Update(modelResource);

                    #endregion
                    ChinaSoft.Common.MessageBox.Show(this, "更新成功！");
                    BindTreeView();
            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnCancel_Click(object sender, ImageClickEventArgs e)
        {
            ControlBtn(true, true, true, false, false);
            ControlTextBox(true, true,true,false,false,true);
        }

        /// <summary>
        /// 控制文本框
        /// </summary>
        /// <param name="txtNameDisabled"></param>
        /// <param name="txtCodeDisabled"></param>
        /// <param name="boolClear"></param>
        private void ControlTextBox(bool txtNameDisabled, bool txtCodeDisabled,bool txtSortDisabled,bool ddlTypeEnabled,bool txtURLEnabled, bool boolClear)
        {
            if (boolClear)
            {
                txtName.Value = "";
                txtCode.Value = "";
                txtSortNum.Value = "";
                txtURL.Text = "";
                ddlResourceType.SelectedValue = "";
            }
            txtName.Disabled = txtNameDisabled;
            txtCode.Disabled = txtCodeDisabled;
            txtSortNum.Disabled = txtSortDisabled;
            ddlResourceType.Enabled = ddlTypeEnabled;
            txtURL.Enabled = txtURLEnabled;
        }
    }
}